<?php

function make1node_lable($nd, $f) {
  static $gtmp = 0;
  $str = $nd->nodeName . $gtmp . " [\n";
  $gtmp += 1;
  $str .= "  label = \"{" . $nd->nodeName . "\n";
  $str .= "    |+ name : string" . "\n";
  $str .= "    \\l+ age : int\n";
  $str .= "    \\l|+ die() : void\\l\n";
  $str .= "  }\"";
  $str .= "]\n\n";
  fwrite($f, $str);
}

function make_dot_file_title($f) {
	$str = "digraph graphname {\n";
	$str .= "  nodesep=2.5;\n";
	$str .= "  node [shape = record];\n";
	fwrite($f, $str);
}

function gen_dot_file($gccxml_path) {
	if (!file_exists($gccxml_path)) {
		print($gccxml_path . " do not exists.");
		return;
	}
	
	$xmlDoc = new DOMDocument();
	$xmlDoc->load($gccxml_path);
	$x = $xmlDoc->documentElement;
	
	$dotfile = fopen($gccxml_path . ".dot", "w") or die("Unable to open file!" . $gccxml_path . ".dot");
	make_dot_file_title($dotfile);
	foreach ($x->childNodes AS $item) {
		if(!isset($item->nodeName)) {
			continue;
		}
		if('#text' == $item->nodeName) {
			continue;
		}
		//print "<tagName>:" . $item->nodeName . "\n";
		
		
		make1node_lable($item, $dotfile);
		
		
		foreach($item->attributes as $b) {
			//echo "    <attr>:" . $b->nodeName . ":" . $b->nodeValue . "\n";	
		}
		
		
		foreach($item->childNodes as $ic) {
			if(!isset($ic->nodeName)) {
				continue;
			}
			if('#text' == $ic->nodeName) {
				continue;
			}
			//print "        <childTag>:" . $ic->nodeName . "\n";
			
			foreach($ic->attributes as $ica) {
				//echo "            <attr>:" . $ica->nodeName . ":" . $ica->nodeValue . "\n";	
			}
		}
	}
	fwrite($dotfile, "}\n");
	fclose($dotfile);
}
//gen_dot_file("upload/demo.xml");
?>